<html>
<head><meta charset="utf-8"><title>NonZero* layout optimization · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html">NonZero* layout optimization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213435613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213435613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213435613">(Oct 15 2020 at 14:39)</a>:</h4>
<p>Hello everyone!</p>
<p>Is there a specific reason for an enum with two variants where each is holding  a <code>NonZeroUsize</code> to not be optimized to the same size as a <code>usize</code>?<br>
In my mind, we could use 1bit for the discriminant and the rest of the bits for the <code>NonZeroUsize</code>: <br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2dbd99c0afe54fbf07fea63a54a5aa62">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2dbd99c0afe54fbf07fea63a54a5aa62</a></p>



<a name="213435751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213435751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213435751">(Oct 15 2020 at 14:40)</a>:</h4>
<p>there isn't any free bit in there, just the value 0 is free</p>



<a name="213435892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213435892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213435892">(Oct 15 2020 at 14:41)</a>:</h4>
<p>ah<br>
silly me</p>



<a name="213435909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213435909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213435909">(Oct 15 2020 at 14:41)</a>:</h4>
<p>of course</p>



<a name="213440220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213440220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213440220">(Oct 15 2020 at 15:10)</a>:</h4>
<p>Thanks :)</p>



<a name="213441563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213441563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213441563">(Oct 15 2020 at 15:17)</a>:</h4>
<p>Even if there were free bits with some data type, references need to work normally. A <code>&amp;T</code> may look corrupt if some bits were set for the discriminator, and <code>&amp;mut T</code> could clobber it.</p>



<a name="213443906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213443906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213443906">(Oct 15 2020 at 15:33)</a>:</h4>
<p>Oh ok that make sense</p>



<a name="213444234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213444234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213444234">(Oct 15 2020 at 15:36)</a>:</h4>
<p>Is there an equivalent to NonZero for  maximum? Like <code>NonMaxUsize</code>. It happens sometime that I store some indexes and such index can never be usize::MAX because that would mean we have allocated usize+1 elements (if I am correct).</p>



<a name="213445110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213445110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213445110">(Oct 15 2020 at 15:42)</a>:</h4>
<p>do you mind using nightly for this?</p>



<a name="213445234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213445234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213445234">(Oct 15 2020 at 15:42)</a>:</h4>
<p>there is <code>#[rustc_layout_scalar_valid_range_end(...)]</code> which does this, but we don't yet have a stable alternative here</p>



<a name="213445435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213445435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213445435">(Oct 15 2020 at 15:44)</a>:</h4>
<p>i think that we will probably end up with something like <code>UsizeValidRange&lt;const L: usize, const H: usize&gt;(usize)</code> in the distant future</p>



<a name="213448204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213448204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213448204">(Oct 15 2020 at 16:02)</a>:</h4>
<p>Would be cool to have a <code>Index&lt;T&gt;</code> type that's <code>[0, usize::MAX / max(1, sizeof(T))]</code>.  Because for non-ZSTs it's not just <code>usize::MAX</code> that's impossible.</p>



<a name="213451867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213451867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213451867">(Oct 15 2020 at 16:32)</a>:</h4>
<p>Yeah that would be really great!</p>



<a name="213452901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213452901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213452901">(Oct 15 2020 at 16:40)</a>:</h4>
<p>Could such a type rely internally (ie: in std) to some unstable feature, but being exposed at some point in stable without actually stabilizing the underlying feature?</p>



<a name="213453397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213453397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213453397">(Oct 15 2020 at 16:43)</a>:</h4>
<p>And could it be implemented like that:</p>
<div class="codehilite"><pre><span></span><code>struct BoundedIndex&lt;T&gt;(
     #[rustc_layout_scalar_valid_range_end(usize::MAX / max(1, sizeof(T)))]
    index: usize,
);
</code></pre></div>


<p>?</p>



<a name="213454564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213454564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213454564">(Oct 15 2020 at 16:51)</a>:</h4>
<p>I tried to get that into <code>[T]::len</code> itself as a hint, not a type, but that lead to some compile time regressions.<br>
Might be interesting to see if those regressions disappear if the repr were a different type instead of <code>assume</code> on load.</p>



<a name="213454717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213454717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213454717">(Oct 15 2020 at 16:52)</a>:</h4>
<p>A stricter limit is <code>isize::MAX / size_of::&lt;T&gt;()</code> for sized types, and <code>usize::MAX</code> for ZSTs.</p>



<a name="213455009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455009">(Oct 15 2020 at 16:54)</a>:</h4>
<p><code>rustc_index</code> defines <code>u32</code>-based newtype indexes, and uses that valid range attribute to expose a niche:<br>
<a href="https://github.com/rust-lang/rust/blob/7f587168102498a488abf608a86c7fdfa62fb7bb/compiler/rustc_index/src/vec.rs#L108-L111">https://github.com/rust-lang/rust/blob/7f587168102498a488abf608a86c7fdfa62fb7bb/compiler/rustc_index/src/vec.rs#L108-L111</a></p>



<a name="213455435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455435">(Oct 15 2020 at 16:57)</a>:</h4>
<p>Does the attribute work with non-literal arguments?</p>



<a name="213455588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455588">(Oct 15 2020 at 16:58)</a>:</h4>
<p>I don't know</p>



<a name="213455715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455715">(Oct 15 2020 at 16:59)</a>:</h4>
<p>BTW, <code>MAX</code> <em>is</em> a valid index for ZSTs when slicing is involved, as the exclusive end of a range</p>



<a name="213455958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455958">(Oct 15 2020 at 17:01)</a>:</h4>
<p>Does not seem so:</p>
<div class="codehilite"><pre><span></span><code>error: expected unsuffixed literal or identifier, found `&lt;`
 --&gt; src/main.rs:9:57
  |
9 |      #[rustc_layout_scalar_valid_range_end(compute_len::&lt;T&gt;())]
  |                                                         ^
</code></pre></div>


<p>for <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=44061c06bce0973eb7b55e602334a198">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=44061c06bce0973eb7b55e602334a198</a></p>



<a name="213455959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213455959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213455959">(Oct 15 2020 at 17:01)</a>:</h4>
<p>Oh, is the attribute not inclusive?</p>



<a name="213456122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213456122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213456122">(Oct 15 2020 at 17:02)</a>:</h4>
<p>I think the attribute is inclusive</p>



<a name="213456204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213456204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213456204">(Oct 15 2020 at 17:03)</a>:</h4>
<p>I just meant that in general, when considering the limits of your indices, the exclusive end may matter too</p>



<a name="213456526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213456526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213456526">(Oct 15 2020 at 17:05)</a>:</h4>
<p>Such a type would be very useful</p>



<a name="213457547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213457547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213457547">(Oct 15 2020 at 17:13)</a>:</h4>
<p>Those inclusive bounds were accepted once, except for the compile time regression: <a href="https://github.com/rust-lang/rust/pull/77023/files#diff-e8ccaf64ce21f955ccebef33b52158631493a6f0966815a2ebc142d7cd2b5e06R84-R90">https://github.com/rust-lang/rust/pull/77023/files#diff-e8ccaf64ce21f955ccebef33b52158631493a6f0966815a2ebc142d7cd2b5e06R84-R90</a></p>



<a name="213459179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213459179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213459179">(Oct 15 2020 at 17:26)</a>:</h4>
<p>That's not exactly the same thing though?</p>



<a name="213459291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213459291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213459291">(Oct 15 2020 at 17:26)</a>:</h4>
<p>In my case, I want a usize-like type that does not use all the possible values, to be able to store a <code>Option&lt;BoundedIndex&lt;T&gt;&gt;</code> to fit in 8 bytes like a <code>usize</code></p>



<a name="213459843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213459843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213459843">(Oct 15 2020 at 17:30)</a>:</h4>
<p>The type is not the same but the bound should be.<br>
The possible value range of <code>len()</code> is the same as that of indices.</p>



<a name="213473502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonZero%2A%20layout%20optimization/near/213473502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonZero*.20layout.20optimization.html#213473502">(Oct 15 2020 at 19:09)</a>:</h4>
<p>I'm curious about the blockers for stable layout optimizations; I'll start a new thread for it, though.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>